<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html lang="zh">
<head>
<%@include file="/WEB-INF/jsp/common/header.jsp"%>
<style type="text/css">
.addForm {
	width: 120px;
}
</style>
<script type="text/javascript">
	var userListTable;
	$(document).ready(function() {
		$("#resetForm").click(function() {
			$("#form :input").val("");
		});
		$("#save").click(function() {
			var id = $(".modal-body input[name=id]").val();
			var loginName = $(".modal-body input[name=loginName]").val();
			var password = $(".modal-body input[name=password]").val();
			var password2 = $(".modal-body input[name=password2]").val();
			var name = $(".modal-body input[name=name]").val();
            var telephone = $(".modal-body input[name=telephone]").val();
            var isLogin = $(".modal-body input[name=isLogin]:checked").val();

			if ($.trim(loginName) == "") {
				$.messager.alert("登录名不能为空");
				return false;
			}
			if(id === ''){
                if ($.trim(password) == "") {
                    $.messager.alert("密码不能为空");
                    return false;
                }
                if ($.trim(password2) == "") {
                    $.messager.alert("确认密码不能为空");
                    return false;
                }
                if($.trim(password) != "" && $.trim(password) != $.trim(password2)){
                    $.messager.alert("二次输入的密码不一致");
                    return false;
                }
            }else{
                if($.trim(password) !== "" || $.trim(password) !== ""){
                    if ($.trim(password) == "") {
                        $.messager.alert("密码不能为空");
                        return false;
                    }
                    if ($.trim(password2) == "") {
                        $.messager.alert("确认密码不能为空");
                        return false;
                    }
                    if($.trim(password) != "" && $.trim(password) != $.trim(password2)){
                        $.messager.alert("二次输入的密码不一致");
                        return false;
                    }
                }
            }

			var url = contextpath + "/web/sysUser/update";
            if(id === ''){
                url = contextpath + "/web/sysUser/insert";
            }
			$(this).attr("disabled","disabled");
			$.ajax({
				type : "POST",
				url : url,
				data : $('#editForm').serialize(),
				success : function(result) {
                    if (result.code === 200) {
                        $("#save").removeAttr("disabled");
                        $("#editModal").modal("hide");
                        $.messager.alert("保存成功");
                        userListTable.ajax.reload(null, false);
                    } else {
                        $.messager.alert(result.msg);
                    }
				}
			});
		});
        userListTable = $('#userListTable').DataTable({
            "bSort": true, //是否启动各个字段的排序功能
            "aaSorting": [[5, "desc"]], //默认的排序方式
            "aoColumns": [
                {
                    "mDataProp": "loginName",
                    "sTitle":"登录名称",
                    "sDefaultContent": "--",
                    "sWidth": "20%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : true,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "name",
                    "sTitle":"名称",
                    "sDefaultContent": "--",
                    "sWidth": "10%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "isLogin",
                    "sTitle":"是否允许登录",
                    "sDefaultContent": "--",
                    "sWidth": "80px",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData === 0)$(nTd).html("否");
                        else $(nTd).html("是");

                    }
                },
                {
                    "mDataProp": "telephone",
                    "sTitle":"联系电话",
                    "sDefaultContent": "--",
                    "sWidth": "20%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "lastLoginTime",
                    "sTitle":"最后登录时间",
                    "sDefaultContent": "--",
                    "sWidth": "15%",
                    "sClass": "center flow",
                    "bSortable": false,
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "createTime",
                    "sTitle": "创建时间",
                    "sWidth": "15%",
                    "sDefaultContent": "--",
                    "bSortable": true,
                    "sClass": "center flow",
                    "bSearchable": false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if (sData !== '--') $(nTd).attr('title', sData);
                    }
                },
                {
                    "mDataProp": "id",
                    "sTitle": "操作",
                    "sWidth": "120px",
                    "sClass": "center flow",
                    "bSearchable": false,
                    "bSortable": false,
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a class='btn btn-xs btn-default' href='javascript:void(0);' onclick='editUser(" + iRow + ")'><i class='fa fa-pencil'></i>&nbsp;编辑</a>&nbsp;&nbsp;")
                            .append("<a class='btn btn-xs btn-default' href='javascript:void(0);' onclick='deleteUser(" + iRow + ")'><i class='fa fa-times'></i>&nbsp;删除</a>&nbsp;&nbsp;");

                    }
                }
            ],
            "ajax": {
                url: contextpath + '/web/sysUser/queryPage',
                type:"post",
                data: function(data){
                    $.xDataTablesParams(data, $.trim($('#loginName').val()));
                    return data;
                }
            }
        });

        $('#editModal').on('hide.bs.modal', function () {
            $('#editModal :input[name=loginName]').removeAttr('disabled');
            $('#editForm input').val('');
        });
    });

    function editUser(row){
        $('#modalTitle').text('编辑用户信息');
        var rowData = userListTable.row(row).data();
        $('#editModal :input[name=id]').val(rowData.id);
        $('#editModal :input[name=loginName]').val(rowData.loginName).attr('disabled', 'disabled');
        $('#editModal :input[name=name]').val(rowData.name);
        $('#editModal :input[name=telephone]').val(rowData.telephone);
        $('#editModal :input[name=isLogin][value='+ rowData.isLogin +']').attr('checked','checked');
        $('#editModal :input[name=password]').val('');
        $('#editModal :input[name=password2]').val('');
        $('#editModal').modal('show');
    }

    function addUser(){
        $('#modalTitle').text('新增用户信息');
        $('#editModal :input[name=isLogin][value="1"]').attr('checked','checked');
        $('#editModal').modal('show');
    }

    function deleteUser(row){
        var rowData = userListTable.row(row).data();
        $.messager.confirm("确定", "真的要删除'" + rowData.loginName + "'吗？", function(){
            $.ajax({
               url : contextpath + '/web/sysUser/delete/' + rowData.id,
               type : 'DELETE',
               success :  function(data){
                   if (data.code === 200) {
                       $.messager.alert("删除成功");
                       userListTable.ajax.reload();
                   } else {
                       $.messager.alert(data.msg);
                   }
               }
            });
        });
    }

    function searchTable(){
        userListTable.ajax.reload();
    }

</script>
</head>
<body>
	<div class="container-fluid">
		<div class="sub-header">
			<h2>管理员列表</h2>
		</div>
		<br/>
		<form id="form" class="form-inline" role="form" action="#" method="post">
			<div class="form-group">
				<div class="input-group">
					<span class="input-group-addon">登录名</span>
					<input type="text" class="form-control" id="loginName" placeholder="登录名" style="width: 150px;">
				</div>
			</div>
			<button type="button" class="btn btn-default" onclick="searchTable()">查询</button>
			<button id="add" type="button" class="btn btn-info" onclick="addUser()">新增</button>
		</form>
		<br>
		<div>
			<table class="table table-hover table-striped table-bordered" style="table-layout:fixed;" id="userListTable"></table>
		</div>
	</div>

	<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg" style="width: 600px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
					</button>
					<h4 class="modal-title" id="modalTitle">用户信息</h4>
				</div>
				<div class="modal-body">
                    <form id="editForm">
                        <input type="hidden" name="id">
                        <table id="editTable" class="table-hover table table-bordered">
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">登录名</span>
                                        <input type="text" class="form-control" name="loginName" placeholder="账户名" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">名称</span>
                                        <input type="text" class="form-control" name="name" placeholder="名称" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">联系电话</span>
                                        <input type="text" class="form-control" name="telephone" placeholder="联系电话" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr id="password">
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">密码</span>
                                        <input type="password" class="form-control" name="password" placeholder="密码" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr id="password2">
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">确认密码</span>
                                        <input type="password" class="form-control" name="password2" placeholder="确认密码" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <table class="table table-bordered table-hover">
                                        <tr>
                                            <td style="width:120px;background:#eee;text-align: center;">是否允许登录</td>
                                            <td>
                                                <div class="checkbox" style="margin-top:0;padding-top:0;">
                                                    <label><input type="radio" name="isLogin" value="0">否</label>&nbsp;&nbsp;
                                                    <label><input type="radio" name="isLogin" value="1">是</label>&nbsp;&nbsp;
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td align="center">
                                    <button id="save" type="button" class="btn btn-success addForm">保 存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <button id="closeModal" type="button" onclick="$('#editModal').modal('hide');" class="btn btn-danger addForm">关闭</button>
                                </td>
                            </tr>
                        </table>
                    </form>
				</div>
			</div>
		</div>
	</div>


</body>